package common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.dao.JdbcDAO;
import common.pojo.SysAdmin;
import common.utils.MD5Util;

@Scope("prototype")
@Repository
public class AdminDAO extends JdbcDAO {
	
	private final String TABLE = "sys_admin";
	
	@SuppressWarnings("unchecked")
	public List<SysAdmin> findList(int start, int limit){
		String sql ="SELECT * FROM "+ 
		"(SELECT *, ROW_NUMBER() OVER (ORDER BY  id ) as rowNum "+
		"FROM "+TABLE+  
		") AS T "+ 
		"WHERE T.RowNum>"+start+ "and T.RowNum<="+(start+limit)+" order by id";
		return getList(sql,null,new AdminRowMapper());
	}
	public SysAdmin findByName(String loginName)
	{
		class RowMapper implements org.springframework.jdbc.core.RowMapper {
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				SysAdmin admin = new SysAdmin();
				admin.setId(rs.getInt(1));
				admin.setLoginName(rs.getString(2));
				admin.setPassword(rs.getString(3));
				admin.setPermit(rs.getInt(4));
				return admin;
			}
		}
		String sql="select id,loginName,password,permit"+
					" from "+TABLE+" where loginName='"+loginName+"'";
		return (SysAdmin) getObject(sql, null, new RowMapper());
	 
		
	}
	
	public int getCount(){
		return getCount("select count(*) from "+TABLE,null);
	}
	

	public SysAdmin findById(int id) {
		String sql="select id,loginName,password,lastLoginIp,lastLoginTime,email,signature,remark,permit"+
					" from "+TABLE+" where id="+id;
		return (SysAdmin) getObject(sql, null, new AdminRowMapper());
	}
	
	
	private class AdminRowMapper implements org.springframework.jdbc.core.RowMapper {

		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			SysAdmin admin=new SysAdmin();
			admin.setId(rs.getInt(1));
			admin.setLoginName(rs.getString(2));
			admin.setPassword(rs.getString(3));
			admin.setLastLoginIp(rs.getString(4));
			admin.setLastLoginTime(rs.getTimestamp(5));
			admin.setEmail(rs.getString(6));
			admin.setSignature(rs.getString(7));
			admin.setRemark(rs.getString(8));
			admin.setPermit(rs.getInt(9));
			return admin;
		}
	}


	public boolean update(SysAdmin admin) {
		// TODO Auto-generated method stub
		String sql="update "+TABLE+" set lastLoginTime=?,lastLoginIp=? where id=?";
		update(sql, new Object[] {admin.getLastLoginTime(),admin.getLastLoginIp(),admin.getId()});
		return true;
	}


	public boolean updateAdmin(SysAdmin admin) {
		// TODO Auto-generated method stub
		String sql="update "+TABLE+" set email=?,signature=?,remark=? where id=?";
		update(sql, new Object[] {admin.getEmail(),admin.getSignature(),admin.getRemark(),admin.getId()});
		return true;
	}


	public boolean updatepwd(SysAdmin admin) {
		String sql="update "+TABLE+" set password=? where id=?";
		update(sql, new Object[] {admin.getPassword(),admin.getId()});
		return true;
	}

	public boolean saveAdmin(SysAdmin admin) {
		String sql="insert into "+TABLE+"(loginName,password,email,signature,remark,permit) values(?,?,?,?,?,2)";
		save(sql, new Object[] {admin.getLoginName(),MD5Util.md5(admin.getPassword()),admin.getEmail(),admin.getSignature(),admin.getRemark()});
		return true;
	}
	public boolean delete(int id) {
		String sql ="delete from "+TABLE+" where id = "+id;
		delete(sql);
		return true;
		
	}

}